//
//  MBSpaceHeaderView.m
//  mibaotu
//
//  Created by admin on 16/9/14.
//  Copyright © 2016年 pegnk. All rights reserved.
//

#import "MBSpaceHeaderView.h"

@interface MBSpaceHeaderView () {
    UIView                      *topView;
    UIButton                    *_loginBt; //立即登录
    UIView                      *_userView;
    UIImageView                 *_userImgView;
    UILabel                     *_nickNameLb;
    UILabel                     *_userDetailLb;
    UIImageView                 *rightView;
    UIButton                    *userBt;
    
    UIView                      *bottomView;
    UILabel                     *_downingLb;
    UILabel                     *_downedLb;
    UILabel                     *_usedLb;
}

@end
@implementation MBSpaceHeaderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor clearColor];
    
    //**********************头部显示登录或者个人信息****************************
    topView = [[UIView alloc]initWithFrame:CGRectMake(0, 20*PERCEN, self.width, 120*PERCEN)];
    topView.backgroundColor = [UIColor whiteColor];
    [self addSubview:topView];
    
    _loginBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _loginBt.frame = CGRectMake((self.width-200*PERCEN)/2., 30*PERCEN, 200*PERCEN, 60*PERCEN);
    _loginBt.layer.cornerRadius = 5.f;
    _loginBt.layer.masksToBounds = YES;
    _loginBt.layer.borderWidth = 1.f;
    _loginBt.layer.borderColor = TINK_BG_COLOR.CGColor;
    [_loginBt setTitle:@"立即登录" forState:UIControlStateNormal];
    [_loginBt setTitle:@"立即登录" forState:UIControlStateHighlighted];
    [_loginBt setTitleColor:TINK_BG_COLOR forState:UIControlStateNormal];
    [_loginBt setTitleColor:TINK_BG_COLOR forState:UIControlStateHighlighted];
    _loginBt.titleLabel.font = [UIFont systemFontOfSize:13.f];
    [_loginBt addTarget:self action:@selector(onLoginAction) forControlEvents:UIControlEventTouchUpInside];
    _loginBt.hidden = ([UserManager standerDefult].userModel.UserID ? YES : NO);
    [topView addSubview:_loginBt];
    
    //个人信息
    _userView = [[UIView alloc]initWithFrame:topView.bounds];
    _userView.backgroundColor = [UIColor clearColor];
    _userView.hidden = !_loginBt.isHidden;
    [topView addSubview:_userView];
    
    _userImgView = [[UIImageView alloc]initWithFrame:CGRectMake((ISIPAD?460*PERCEN:25*PERCEN), 15*PERCEN, 90*PERCEN, 90*PERCEN)];
    _userImgView.backgroundColor = [UIColor clearColor];
    [_userView addSubview:_userImgView];
    
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectMake(_userImgView.right+20*PERCEN, 22*PERCEN, 200, 20)];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x333333);
    _nickNameLb.font = [UIFont systemFontOfSize:14.f];
    [_userView addSubview:_nickNameLb];
    
    _userDetailLb = [[UILabel alloc]initWithFrame:CGRectMake(_nickNameLb.left, (WIDTH_5S ? _nickNameLb.bottom -2: _userView.height-22*PERCEN-20), 550*PERCEN, 20)];
    _userDetailLb.backgroundColor = [UIColor clearColor];
    _userDetailLb.textAlignment = NSTextAlignmentLeft;
    _userDetailLb.textColor = UIColorFromHex(0x333333);
    _userDetailLb.font = [UIFont systemFontOfSize:12.f];
    [_userView addSubview:_userDetailLb];
    
    UIImage *rightImg = [UIImage imageNamed:@"arrow"];
    rightView= [[UIImageView alloc]initWithFrame:CGRectMake(_userView.right-(ISIPAD?460*PERCEN:25*PERCEN)-rightImg.size.width, (120*PERCEN-rightImg.size.height)/2., rightImg.size.width, rightImg.size.height)];
    rightView.backgroundColor = [UIColor clearColor];
    rightView.image = rightImg;
    [_userView addSubview:rightView];
    
    userBt = [UIButton buttonWithType:UIButtonTypeCustom];
    userBt.backgroundColor = [UIColor clearColor];
    userBt.frame = _userView.bounds;
    [userBt addTarget:self action:@selector(onSelectForUserAction) forControlEvents:UIControlEventTouchUpInside];
    [_userView addSubview:userBt];
    
    //**********************尾部显示正在下载、已下载、最近播放****************************
    bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, topView.bottom+20*PERCEN, self.width, 140*PERCEN)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bottomView];
    
    float itemWidth = bottomView.width / 3.;
    if (ISIPAD) {
        itemWidth = 390*PERCEN;
    }
    for (int i=0; i<3; i++) {
        UIImageView *headImgView = [[UIImageView alloc]initWithFrame:CGRectMake((itemWidth-15)/2.+itemWidth*i, 14*PERCEN, 15, 17)];
        headImgView.backgroundColor = [UIColor clearColor];
        [bottomView addSubview:headImgView];
        
        UILabel *titleLb = [[UILabel alloc]initWithFrame:CGRectMake(itemWidth*i, (WIDTH_5S ? headImgView.bottom+2 :headImgView.bottom+10*PERCEN), itemWidth, 20)];
        titleLb.backgroundColor = [UIColor clearColor];
        titleLb.textAlignment = NSTextAlignmentCenter;
        titleLb.textColor = UIColorFromHex(0x333333);
        titleLb.font = [UIFont systemFontOfSize:13.f];
        [bottomView addSubview:titleLb];
        
        UILabel *titleDetailLb = [[UILabel alloc]initWithFrame:CGRectMake(itemWidth*i, (WIDTH_5S ? titleLb.bottom-7 : titleLb.bottom-2*PERCEN), itemWidth, 20)];
        titleDetailLb.backgroundColor = [UIColor clearColor];
        titleDetailLb.textAlignment = NSTextAlignmentCenter;
        titleDetailLb.textColor = UIColorFromHex(0x333333);
        titleDetailLb.font = [UIFont systemFontOfSize:12.f];
        [bottomView addSubview:titleDetailLb];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(itemWidth-1 + itemWidth*i, 20*PERCEN, 1, 100*PERCEN)];
        lineView.backgroundColor = UIColorFromHex(0xcccccc);
        [bottomView addSubview:lineView];
        
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.frame = CGRectMake(itemWidth*i, 0, itemWidth, bottomView.height);
        bt.backgroundColor = [UIColor clearColor];
        [bottomView addSubview:bt];
        
        if (i == 0) {
            UIImage *img = [UIImage imageNamed:@"zhengzaixiazai_kj"];
            headImgView.image = img;
            headImgView.frame = CGRectMake((itemWidth-img.size.width)/2.+itemWidth*i, 14*PERCEN, img.size.width, img.size.height);
            titleLb.text = @"正在下载";
            titleDetailLb.text = @"0首";
            _downingLb = titleDetailLb;
            [bt addTarget:self action:@selector(onSelectDowningAction) forControlEvents:UIControlEventTouchUpInside];
            
            headImgView.tag = 10;
            titleLb.tag = 13;
            titleDetailLb.tag = 16;
            lineView.tag = 19;
            bt.tag = 22;
            if (ISIPAD) {
                headImgView.left = (self.width-3*itemWidth)/2.+(itemWidth-img.size.width)/2.+itemWidth*i;
                titleLb.left = (self.width-3*itemWidth)/2.+ itemWidth*i;
                titleDetailLb.left = titleLb.left;
                lineView.left = titleLb.right;
                bt.left = (self.width-3*itemWidth)/2.+ itemWidth*i;
            }
        }
        else if (i == 1) {
            UIImage *img = [UIImage imageNamed:@"yixiazai_kj"];
            headImgView.image = img;
            headImgView.frame = CGRectMake((itemWidth-img.size.width)/2.+itemWidth*i, 14*PERCEN, img.size.width, img.size.height);
            titleLb.text = @"已下载";
            titleDetailLb.text = @"0首";
            _downedLb = titleDetailLb;
            [bt addTarget:self action:@selector(onSelectDownedAction) forControlEvents:UIControlEventTouchUpInside];
            
            headImgView.tag = 11;
            titleLb.tag = 14;
            titleDetailLb.tag = 17;
            lineView.tag = 20;
            bt.tag = 23;
            if (ISIPAD) {
                headImgView.left = (self.width-3*itemWidth)/2.+(itemWidth-img.size.width)/2.+itemWidth*i;
                titleLb.left = (self.width-3*itemWidth)/2.+ itemWidth*i;
                titleDetailLb.left = titleLb.left;
                lineView.left = titleLb.right;
                bt.left = (self.width-3*itemWidth)/2.+ itemWidth*i;
            }
        }
        else if (i == 2) {
            UIImage *img = [UIImage imageNamed:@"zuijinbofang_kj"];
            headImgView.image = img;
            headImgView.frame = CGRectMake((itemWidth-img.size.width)/2.+itemWidth*i, 14*PERCEN, img.size.width, img.size.height);
            titleLb.text = @"最近播放";
            titleDetailLb.text = @"0首";
            _usedLb = titleDetailLb;
            lineView.hidden = YES;
            [bt addTarget:self action:@selector(onSelectUseAction) forControlEvents:UIControlEventTouchUpInside];
            
            headImgView.tag = 12;
            titleLb.tag = 15;
            titleDetailLb.tag = 18;
            lineView.tag = 21;
            bt.tag = 24;
            if (ISIPAD) {
                headImgView.left = (self.width-3*itemWidth)/2.+(itemWidth-img.size.width)/2.+itemWidth*i;
                titleLb.left = (self.width-3*itemWidth)/2.+ itemWidth*i;
                titleDetailLb.left = titleLb.left;
                lineView.left = titleLb.right;
                bt.left = (self.width-3*itemWidth)/2.+ itemWidth*i;
            }
        }
    }
}

//立即登录
- (void)onLoginAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForLoginWithView:)]) {
        [_delegate onSelectItemForLoginWithView:self];
    }
}

- (void)onSelectForUserAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(onselectItemForUserWithView:)]) {
        [_delegate onselectItemForUserWithView:self];
    }
}

//正在下载
- (void)onSelectDowningAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectITemForDowningWithView:)]) {
        [_delegate onSelectITemForDowningWithView:self];
    }
}

//已下载
- (void)onSelectDownedAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectITemForDownedWithView:)]) {
        [_delegate onSelectITemForDownedWithView:self];
    }
}

//最近播放
- (void)onSelectUseAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectITemForUsedWithView:)]) {
        [_delegate onSelectITemForUsedWithView:self];
    }
}

- (void)setText:(BOOL)text
{
    _text = text;
    [self setNeedsLayout];
}

- (void)setDowningMusic:(NSString *)downingMusic
{
    _downingMusic = downingMusic;
    [self setNeedsLayout];
}

- (void)setDownedMusic:(NSString *)downedMusic
{
    _downedMusic = downedMusic;
    [self setNeedsLayout];
}

- (void)setPlayedMusic:(NSString *)playedMusic
{
    _playedMusic = playedMusic;
    [self setNeedsLayout];
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (_text) {
        _userView.hidden = NO;
        _loginBt.hidden = YES;
    } else {
        _userView.hidden = YES;
        _loginBt.hidden = NO;
    }
    UserModel *model = [UserManager standerDefult].userModel;
    if (model) {
        if ([model.FaceImg hasPrefix:@"/upload/"]) {
            [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",MUSIC_PLAY_Url,model.FaceImg]] placeholderImage:[UIImage imageNamed:@"story_header_bg"]];
        } else {
            [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.FaceImg]] placeholderImage:[UIImage imageNamed:@"story_header_bg"]];
        }
        if (model.FaceImg.length > 0) {
            _userImgView.layer.cornerRadius = _userImgView.height / 2.;
            _userImgView.layer.masksToBounds = YES;
        }
        _nickNameLb.text = (model.NickName.length > 0 ? model.NickName:@"米宝兔");
        [_nickNameLb.text stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        _userDetailLb.text = (model.Signature.length == 0 ? @"宝宝爱吃爱喝爱睡觉，不服你来比一比!哼哼。" : model.Signature);
    }
    _downingLb.text = [NSString stringWithFormat:@"%@首",_downingMusic];
    _downedLb.text = [NSString stringWithFormat:@"%@首",_downedMusic];
    _usedLb.text = [NSString stringWithFormat:@"%@首",_playedMusic];
    
    //frame
        topView.frame = CGRectMake(0, 20*PERCEN, self.width, 120*PERCEN);
        _loginBt.frame = CGRectMake((self.width-200*PERCEN)/2., 30*PERCEN, 200*PERCEN, 60*PERCEN);
        _userView.frame = topView.bounds;
        _userImgView.frame = CGRectMake((ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), 15*PERCEN, 90*PERCEN, 90*PERCEN);
        _nickNameLb.frame = CGRectMake(_userImgView.right+20*PERCEN, 22*PERCEN, 200, 20);
        _userDetailLb.frame = CGRectMake(_nickNameLb.left, (WIDTH_5S ? _nickNameLb.bottom -2: _userView.height-22*PERCEN-20), 550*PERCEN, 20);
        UIImage *rightImg = [UIImage imageNamed:@"arrow"];
        rightView.frame = CGRectMake(_userView.right-(ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN)-rightImg.size.width, (120*PERCEN-rightImg.size.height)/2., rightImg.size.width, rightImg.size.height);
        userBt.frame = _userView.bounds;
        
        bottomView.frame = CGRectMake(0, topView.bottom+20*PERCEN, self.width, 140*PERCEN);
        
        float itemWidth = bottomView.width / 3.;
        if (ISIPAD) {
            itemWidth = 390*PERCEN;
        }
        for (int i=0; i<3; i++) {
            UIImageView *headImgView = (UIImageView *)[self viewWithTag:10+i];
            UILabel *titleLb = (UILabel *)[self viewWithTag:13+i];
            UILabel *titleDetailLb = (UILabel *)[self viewWithTag:16+i];
            UIView *lineView = (UIView *)[self viewWithTag:19+i];
            UIButton *bt = (UIButton *)[self viewWithTag:22+i];
            
            if (i == 0) {
                UIImage *img = [UIImage imageNamed:@"zhengzaixiazai_kj"];
                if (ISIPAD) {
                    headImgView.frame = CGRectMake((self.width-3*itemWidth)/2.+(itemWidth-img.size.width)/2.+itemWidth*i, 14*PERCEN, img.size.width, img.size.height);
                } else {
                    headImgView.frame = CGRectMake((itemWidth-img.size.width)/2., 14*PERCEN, img.size.width, img.size.height);
                }
                
            }
            else if (i == 1) {
                UIImage *img = [UIImage imageNamed:@"yixiazai_kj"];
                if (ISIPAD) {
                    headImgView.frame = CGRectMake((self.width-3*itemWidth)/2.+(itemWidth-img.size.width)/2.+itemWidth*i, 14*PERCEN, img.size.width, img.size.height);
                } else {
                     headImgView.frame = CGRectMake((itemWidth-img.size.width)/2.+itemWidth*i, 14*PERCEN, img.size.width, img.size.height);
                }
            }
            else if (i == 2) {
                UIImage *img = [UIImage imageNamed:@"zuijinbofang_kj"];
                if (ISIPAD) {
                    headImgView.frame = CGRectMake((self.width-3*itemWidth)/2.+(itemWidth-img.size.width)/2.+itemWidth*i, 14*PERCEN, img.size.width, img.size.height);
                } else {
                     headImgView.frame = CGRectMake((itemWidth-img.size.width)/2.+itemWidth*i, 14*PERCEN, img.size.width, img.size.height);
                }
                
            }
            if (ISIPAD) {
                titleLb.left = (self.width-3*itemWidth)/2.+ itemWidth*i;
                titleDetailLb.left = titleLb.left;
                lineView.left = titleLb.right;
                bt.left = (self.width-3*itemWidth)/2.+ itemWidth*i;
            } else {
                titleLb.frame = CGRectMake(itemWidth*i, (WIDTH_5S ? headImgView.bottom+2 :headImgView.bottom+10*PERCEN), itemWidth, 20);
                titleDetailLb.frame = CGRectMake(itemWidth*i, titleLb.bottom-2*PERCEN-(WIDTH_5S?4:0), itemWidth, 20);
                lineView.frame = CGRectMake(itemWidth-1 + itemWidth*i, 15*PERCEN, 1, bottomView.height-30*PERCEN);
                bt.frame = CGRectMake(itemWidth*i, 0, itemWidth, bottomView.height);
            }
        }
}

@end
